package com.myk.game.gobangchess.ai.gravityfour;

public class ScorePredictionDeep1ford {
    public static int[] scorePredictionDeep1ford(int[][] board, int direction, int playerTurn, int dirChanged) {
        int[] minScore = {-90000, -90000, -90000, -90000, -90000, -90000, -90000, -90000, -90000, -90000};
        int[] point = new int[1];
        int bestScore = -90000;

        for (int x = 1; x <= 10; x++) {
            int[][] newBoard = new int[6][6];
            for (int i = 0; i < board.length; i++) {
                System.arraycopy(board[i], 0, newBoard[i], 0, board[i].length);
            }
            Object[] pointSettingResult = PointSetting.pointSetting(newBoard, direction, playerTurn, x, dirChanged);
            int setSuccess = (int) pointSettingResult[0];
            newBoard = (int[][]) pointSettingResult[1];
            int newDirection = (int) pointSettingResult[2];
            int newdirChanged = (int) pointSettingResult[3];

            if (setSuccess == 1) {
                int[] isKillDeep3CheckResult = KillJudging3plus.killJudgingDeep3(newBoard, newDirection, -playerTurn, newdirChanged);
                int isKillDeep3Check = isKillDeep3CheckResult[0];
                int killDeepcheck = isKillDeep3CheckResult[2];

                if (isKillDeep3Check == 0) {
                    int[] newScoreInner = {70000, 70000, 70000, 70000, 70000, 70000, 70000, 70000, 70000, 70000};

                    for (int x1 = 1; x1 <= 10; x1++) {
                        int[][] newBoard1 = new int[6][6];
                        for (int i = 0; i < newBoard.length; i++) {
                            System.arraycopy(newBoard[i], 0, newBoard1[i], 0, newBoard[i].length);
                        }

                        Object[] pointSettingResult1 = PointSetting.pointSetting(newBoard1, newDirection, -playerTurn, x1, newdirChanged);
                        int setSuccess1 = (int) pointSettingResult1[0];
                        newBoard1 = (int[][]) pointSettingResult1[1];
                        int newDirection1 = (int) pointSettingResult1[2];
                        int newdirChanged1 = (int) pointSettingResult1[3];

                        if (setSuccess1 == 1) {
                            int[] isKillDeep2CheckResult1 = KillJudging2plus.killJudgingDeep2(newBoard1, newDirection1, playerTurn, newdirChanged1);
                            int isKillDeep2Check1 = isKillDeep2CheckResult1[0];

                            if (isKillDeep2Check1 == 0) {
                                newScoreInner[x1 - 1] = playerTurn * scoreWeighting.scoreWeighting1(newBoard1, playerTurn);
                            } else {
                                newScoreInner[x1 - 1] = 70000;
                            }
                        }
                    }

                    int minScoreX = findMin(newScoreInner);
                    minScore[x - 1] = newScoreInner[minScoreX];
                } else{
                    minScore[x - 1] = -70000 + 10000*killDeepcheck -x;
                }
            } else {
                minScore[x - 1] = -90000;
            }
        }

        int maxIndex = findMax(minScore);
        bestScore = minScore[maxIndex];
        point[0] = maxIndex + 1;

        int[] output = {point[0], bestScore};
        return output;
    }

    private static int findMin(int[] array) {
        int minIndex = 0;
        int minValue = array[0];
        for (int i = 1; i < array.length; i++) {
            if (array[i] < minValue) {
                minValue = array[i];
                minIndex = i;
            }
        }
        return minIndex;
    }

    private static int findMax(int[] array) {
        int maxIndex = 0;
        int maxValue = array[0];
        for (int i = 1; i < array.length; i++) {
            if (array[i] > maxValue) {
                maxValue = array[i];
                maxIndex = i;
            }
        }
        return maxIndex;
    }
}
